package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.resp.RespBrandVo;
import com.goktech.olala.core.resp.RespCategoryVo;
import com.goktech.olala.core.resp.RespGoodsVo;
import com.goktech.olala.core.resp.RespPictureVo;
import com.goktech.olala.core.service.IGoodCategory;
import com.goktech.olala.server.dao.goods.*;
import com.goktech.olala.server.pojo.goods.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname GoodCategoryImp
 * @Description TODO
 * @Date 2020/12/1 20:26
 * @Created by ASUS
 */
@Service
public class GoodCategoryImp implements IGoodCategory {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    GoodsReportMapper goodsReportMapper;
    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    GoodParamMapper goodParamMapper;

    @Override
    public int deleteByPrimaryKey(Integer categoryId) {
        int i = categoryMapper.deleteByPrimaryKey(categoryId);
        return i;
    }

    @Override
    public int insert(Category record) {
        int insert = categoryMapper.insert(record);
        return insert;
    }

    @Override
    public Category selectByPrimaryKey(Integer categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        return category;
    }

    @Override
    public List<Category> selectAll() {
        List<Category> categories = categoryMapper.selectAll();
        return categories;
    }

    @Override
    public int updateByPrimaryKey(Category record) {
        int i = categoryMapper.updateByPrimaryKey(record);
        return i;
    }

    @Override
    public List<RespCategoryVo> selectByCategory() {

        List<RespCategoryVo> firstList = categoryMapper.selectByCategory();

        List<RespCategoryVo> secondList = null;

        List<RespCategoryVo> thireList = null;

        for (RespCategoryVo first : firstList){
            //拿到列
            String categoryIdArr = first.getCategoryIdArr();
            String[] cateIds = categoryIdArr.split(",");
            secondList = new ArrayList<>();
            for (String cateId:cateIds) {
                Integer categoryId = Integer.valueOf(cateId);
                //根据categoryId查询二级目录List
                List<RespCategoryVo> childList = categoryMapper.selectByA(categoryId);
                if (childList!=null){
                    for(RespCategoryVo second:childList){
                        Integer categoryId1 = second.getCategoryId();
                        thireList = categoryMapper.selectByA(categoryId1);
                        second.setChildList(thireList);
                    }
                    secondList.addAll(childList);
                }
//                first.setSecondList(secondList);
            }
            first.setChildList(secondList);
        }

        return firstList;

    }

    @Override
    public List<RespBrandVo> selectBrand() {
        List<RespBrandVo> respBrandVos = brandMapper.selectBrand();
        return respBrandVos;
    }

    /*响应商品详情页面
    * */
    @Override
    public RespGoodsVo queryGoodsById(String goodsId) {
        //商品信息
        Good goods = goodMapper.selectByPrimaryKey(goodsId);
        //商品销量
        GoodsReport goodsReport = goodsReportMapper.selectByPrimaryKey(goodsId);
        //商品图片
        List<Picture> pictureList = pictureMapper.selectByGoodsId(goodsId);
        //商品详情
        GoodParam goodParam = goodParamMapper.selectByGoodsId(goodsId);

        //封装商品信息
        RespGoodsVo respGoods = new RespGoodsVo();
        if (goods!=null){
            //商品基础信息
            respGoods.setGoodsId(goodsId);
            respGoods.setGoodsName(goods.getGoodsName());
            respGoods.setGoodsSn(goods.getGoodsSn());
            respGoods.setGoodsTitle(goods.getGoodsTitle());
            respGoods.setMarketPrice(goods.getMarketPrice());
            respGoods.setPromotePrice(goods.getPromotePrice());
            respGoods.setShopPrice(goods.getShopPrice());
            respGoods.setShipFee(goods.getShipFee());
            //商品销量
            if (goodsReport!=null){
                respGoods.setMonthSales(goodsReport.getMonthSales());
                respGoods.setCountSales(goodsReport.getCountSales());
                respGoods.setCountComms(goodsReport.getCountComms());
            }
            //商品图片
            if (pictureList!=null){
                //封装商品图片
                List<RespPictureVo> pictureVos = new ArrayList<>();
                for (int i=0;i<pictureList.size();i++){
                    RespPictureVo respPictureVo = new RespPictureVo();
                    respPictureVo.setIsMaster(pictureList.get(i).getIsMaster());
                    respPictureVo.setOriginalImg(pictureList.get(i).getOriginalImg());
                    respPictureVo.setPictureTitle(pictureList.get(i).getPictureTitle());
                    respPictureVo.setRealImg(pictureList.get(i).getRealImg());
                    respPictureVo.setRelationId(pictureList.get(i).getRelationId());
                    respPictureVo.setThumbImg(pictureList.get(i).getThumbImg());
                    //把它加进去
                    pictureVos.add(i,respPictureVo);

                }

                //把商品详情加进去
                if (goodParam!=null){
                    respGoods.setGoodParam(goodParam);
                }

                respGoods.setPictureVos(pictureVos);
            }

        }
        return respGoods;
    }
}
